public class test1 {
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        List<List<Integer>> res;
        Deque<TreeNode> stack;
        public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
            res = new ArrayList<>();
            stack = new LinkedList<>();
            if(root==null){
                return res;
            }
            stack.offer(root);
            int num = 0;
            while(!stack.isEmpty()){
                int len = stack.size();
                List<Integer> list = new ArrayList<>();
                for(int i = 0;i<len;i++){
                    TreeNode tmp = stack.pop();
                    list.add(tmp.val);
                    if(tmp.left!=null){
                        stack.offer(tmp.left);
                    }
                    if(tmp.right!=null){
                        stack.offer(tmp.right);
                    }
                }
                if(num%2==0){
                    res.add(list);
                }else{
                    //res.add(list.reverse());
                    //res.add(Collections.reverse(list));
                    Collections.reverse(list);//它是没有返回值的，所以不能是上面的用法
                    res.add(list);
                }
                num++;
            }
            return res;
        }
    }
}
